#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年7月29日

@author: chenyongbing
'''
import sys,os,datetime
import commands
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

rawfile = '/Users/chenyongbing/Documents/workspace/zmWebTools.bak/zmSchedulerTools/log/rawdata.2014-10-09.txt'

    
from mrjob.job import MRJob
class MRRawData(MRJob):    

    def get_time_stamp(self,stime):
        tlist = stime.split(':')
        stamp = int(tlist[0])*3600+int(tlist[1])*60+int(tlist[2])
        return stamp
    def mapper(self, _, line):
        lineList = line.split('.')
        if len(lineList)==6:
            day,device,mac,stime,rssi,rtime = lineList
            stamp = self.get_time_stamp(stime)
            yield '%s'%(device),line
            #yield device,1
#     def combiner(self,key,values):
#         datas = []
#         value = values.next()
#         while value:
#             value = values.next()
#             datas.append(value)
#         yield (key,len(datas))
#         yield (key,sum(values))
#     
    
    def reducer(self,key,values):
        dfile = os.path.join(current_dir,'../log/%s'%key)
        fr = open(dfile,'w')
        fr.write('\n'.join(values))
def filter_by_awk(rawfile=''):
    dpath = os.path.join(current_dir,'../log/')
    os.chdir(dpath)
    
    cmd = "awk -F '.' '{if ($2!=null) print $0 >>$2}' %s"%rawfile
    x = commands.getoutput(cmd)
    print x
if __name__=='__main__':
    print 'start ',datetime.datetime.today()
    #MRRawData.run()
    filter_by_awk(rawfile)
    print 'end ',datetime.datetime.today()